package org.notabug.lifeuser.moviedb;

import android.content.Context;
import android.os.Environment;
import android.os.Build;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.widget.Toast;

import android.util.Log;
import android.Manifest;
import android.content.pm.PackageManager;

import android.support.v4.app.ActivityCompat;

import java.io.File;
import java.nio.channels.FileChannel;
import java.io.FileInputStream;
import java.io.FileOutputStream;

import static android.R.attr.path;

public class MovieDatabaseHelper extends SQLiteOpenHelper {

	public static final String TABLE_MOVIES = "movies";
	public static final String COLUMN_ID = "id";
	public static final String COLUMN_MOVIES_ID = "movie_id";
	public static final String COLUMN_RATING = "rating";
	public static final String COLUMN_PERSONAL_RATING = "personal_rating";
	public static final String COLUMN_IMAGE = "image";
	public static final String COLUMN_ICON = "icon";
	public static final String COLUMN_TITLE = "title";
	public static final String COLUMN_SUMMARY = "summary";
	public static final String COLUMN_GENRES = "genres";
	public static final String COLUMN_GENRES_IDS = "genres_ids";
	public static final String COLUMN_PERSONAL_START_DATE = "personal_start_date";
	public static final String COLUMN_PERSONAL_FINISH_DATE = "personal_finish_date";
	public static final String COLUMN_PERSONAL_REWATCHED = "personal_rewatched";
	public static final String COLUMN_WATCHED = "watched";
	public static final String COLUMN_MOVIE = "movie";

	private static final String DATABASE_NAME = "movies.db";
	private static final int DATABASE_VERSION = 6;

	// Database creation sql statement
	private String DATABASE_CREATE = "CREATE TABLE IF NOT EXISTS " +
			TABLE_MOVIES + "(" + COLUMN_ID + " integer primary key autoincrement, " +
			COLUMN_MOVIES_ID + " integer not null, " + COLUMN_RATING + 
			" integer not null, " + COLUMN_PERSONAL_RATING + " integer, " + 
			COLUMN_IMAGE + " text not null, " + COLUMN_ICON + " text not null, " +
			COLUMN_TITLE + " text not null, " + COLUMN_SUMMARY + " text not null, " +
			COLUMN_GENRES + " text not null, " + COLUMN_GENRES_IDS 
			+ " text not null, " + COLUMN_PERSONAL_START_DATE + " text, " + 
			COLUMN_PERSONAL_FINISH_DATE + " text, " + COLUMN_PERSONAL_REWATCHED + 
			" integer, " + COLUMN_WATCHED + " integer not null, " + COLUMN_MOVIE + 
			" integer not null);";

	// Initialize the database object.
	public MovieDatabaseHelper(Context context) {
		super(context, DATABASE_NAME, null, DATABASE_VERSION);
	}

	public void exportDatabase(Context context) {
		try {
			File path = Environment.getExternalStoragePublicDirectory(
					Environment.DIRECTORY_DOWNLOADS);
			File data = Environment.getDataDirectory();

			if(path.canWrite()) {
				String currentDBPath = "/data/" + context.getPackageName() +
					"/databases/" + DATABASE_NAME;
				String exportDBPath = DATABASE_NAME;
				File currentDB = new File(data, currentDBPath);
				File exportDB = new File(path, exportDBPath);

				FileChannel src = new FileInputStream(currentDB).getChannel();
				FileChannel dst = new FileOutputStream(exportDB).getChannel();
				dst.transferFrom(src, 0, src.size());
				src.close();
				dst.close();
			} else {
				Toast.makeText(context, context.getResources().getString(R.string.write_to_external_storage_failed), Toast.LENGTH_SHORT).show();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void importDatabase(Context context) {
		try {
			File path = Environment.getExternalStoragePublicDirectory(
					Environment.DIRECTORY_DOWNLOADS);
			File data = Environment.getDataDirectory();

			String currentDBPath = "/data/" + context.getPackageName() +
					"/databases/" + DATABASE_NAME;
			Toast.makeText(context, currentDBPath, Toast.LENGTH_SHORT).show();
			Toast.makeText(context, path + "/" + DATABASE_NAME, Toast.LENGTH_SHORT).show();
			String exportDBPath = DATABASE_NAME;
			File currentDB = new File(data, currentDBPath);
			File importDB = new File(path, exportDBPath);

			FileChannel src = new FileInputStream(importDB).getChannel();
			FileChannel dst = new FileOutputStream(currentDB).getChannel();
			dst.transferFrom(src, 0, src.size());
			src.close();
			dst.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@Override
	public void onCreate(SQLiteDatabase database) {
		// Create the database with the database creation statement.
		database.execSQL(DATABASE_CREATE);
	}

	@Override
	public void onUpgrade(SQLiteDatabase database, int oldVersion, int newVersion) {
		// Update the database when the version has changed.
		Log.w(MovieDatabaseHelper.class.getName(), "Upgrading database from version "
				+ oldVersion + " to " + newVersion + 
				", which will destroy all old data");
		database.execSQL("DROP TABLE IF EXISTS " + TABLE_MOVIES);
		onCreate(database);
	}
}
